package com.itboye.zhujia.login;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.text.InputType;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.zhujia.R;
import com.itboye.zhujia.base.BaseActivity;
import com.itboye.zhujia.bean.PersonDataBean;
import com.itboye.zhujia.constants.SPContants;
import com.itboye.zhujia.controller.LoginController;
import com.itboye.zhujia.controller.LoginedState;
import com.itboye.zhujia.network.HttpRequest;
import com.itboye.zhujia.util.DebugLog;
import com.itboye.zhujia.util.SPUtils;
import com.itboye.zhujia.util.T;
import com.itboye.zhujia.volley.MyJsonRequest;
import com.itboye.zhujia.volley.XErrorListener;
import com.itboye.zhujia.volley.XRequestListener;
import com.umeng.socialize.utils.Log;

/**
 * 登录
 * 
 */
public class LoginActivity extends BaseActivity implements OnClickListener{
	ImageView close_icon;//返回图片
	TextView add_shap_title_tv;//标题
	EditText login_pass;// 密码
	EditText login_username;//账号
	TextView login_ok;//登录
	TextView login_forget;//忘记密码
	TextView login_register;//手机注册
	ImageView login_three;//第三方登录
	
	private String phone;
	private String password;
	
	private Context context;
	public static String LOGINEXIT = "com.itboye.zhujia.exit";//登录界面推出指令
	
	@Override
	public int initView() {
		return 0;
	}

	@Override
	public void onMyCreate() {
		setContentView(R.layout.activity_login);
		
		PassPyte();
		
		// 短信登录发送过来的广播
		IntentFilter filter1 = new IntentFilter(LOGINEXIT);
		registerReceiver(emailBroadcastReceiver, filter1);

	}
	@Override
	public void onClick(View v) {
		Intent intetn=null;
		switch (v.getId()) {
		case R.id.close_icon://返回
			finish();
			break;
		case R.id.login_ok://登录
			phone = login_username.getText().toString();
			password = login_pass.getText().toString();
			if (TextUtils.isEmpty(phone)) {

				T.showShort(context, "请输入手机号");
			} else if (TextUtils.isEmpty(password)) {
				T.showShort(context, "请输入密码");
			} else {
				SPUtils.put(LoginActivity.this, null, SPContants.USERNAME, phone);
				SPUtils.put(LoginActivity.this, null, SPContants.PASSWORD, password);
				login();
			}
			
			break;
		case R.id.login_forget://忘记密码
			intetn=new Intent(LoginActivity.this, PasswordActivityOne.class);
			startActivity(intetn);
			break;
		case R.id.login_register://手机注册
			intetn=new Intent(LoginActivity.this, RegisterActivityOne.class);
			startActivity(intetn);
			break;
		case R.id.login_three://第三方登录
	
			break;
			

		default:
			break;
		}
		
	}
	/**
	 * 登录
	 */
	public void login() {
		MyJsonRequest<PersonDataBean> request = new MyJsonRequest.Builder<PersonDataBean>()
				.apiVer("100")
				.typeKey("BY_User_login")
				.param("username",phone)
				.param("password",password)
				.requestListener(new XRequestListener<PersonDataBean>() {

					@Override
					public void onResponse(PersonDataBean response) {
						DebugLog.v("result", response.toString());
						Log.i("result", response.toString());
						SPUtils.put(LoginActivity.this, null, SPContants.WX_Openid,
								response.getWxopenid());
						SPUtils.put(LoginActivity.this, null, SPContants.USERNAME,
								response.getUsername());
						SPUtils.put(LoginActivity.this, null, SPContants.MOBILE,
								response.getMobile());
						SPUtils.put(LoginActivity.this, null, SPContants.IS_LOGINED, true);
						SPUtils.put(LoginActivity.this, null, SPContants.EMAIL,
								response.getEmail());
						SPUtils.put(LoginActivity.this, null, SPContants.NICK,
								response.getNickname());
						SPUtils.put(LoginActivity.this, null, SPContants.PASSWORD,
								response.getPassword());
						SPUtils.put(LoginActivity.this, null, SPContants.USER_ID,
								response.getUid());
						SPUtils.put(LoginActivity.this, null, SPContants.AVATAR_URL,
								response.getHead());
						SPUtils.put(LoginActivity.this, null, SPContants.AQ_NUM,
								response.getAqCount());
						SPUtils.put(LoginActivity.this, null, SPContants.SEX,
								response.getSex());
						LoginActivity.this.finish();
						LoginController.setLoginState(new LoginedState());
						// 发送广播通知更改头像(还未实现)
						Intent intent2 = new Intent("changeavart");
						sendBroadcast(intent2);
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						T.showShort(context, "请求错误");
					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	
	/**
	 * 设置密码可见
	 */
	private void PassPyte() {
		login_pass.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// et.getCompoundDrawables()得到一个长度为4的数组，分别表示左右上下四张图片
				Drawable drawable = login_pass.getCompoundDrawables()[2];
				// 如果右边没有图片，不再处理
				if (drawable == null)
					return false;
				// 如果不是按下事件，不再处理
				if (event.getAction() != MotionEvent.ACTION_UP)
					return false;
				if (event.getX() > login_pass.getWidth()
						- login_pass.getPaddingRight()
						- drawable.getIntrinsicWidth()) {
					login_pass
							.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);// 密码可见
				} else {
					login_pass.setInputType(InputType.TYPE_CLASS_TEXT
							| InputType.TYPE_TEXT_VARIATION_PASSWORD);// 密码不可见
				}
				return false;
			}
		});
	}
	BroadcastReceiver emailBroadcastReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			LoginActivity.this.finish();
		};
	};
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(emailBroadcastReceiver);
	};

}
